package com.hontlong.commons.threadpool;

import java.util.Collection;
import java.util.Vector;

import org.apache.log4j.Logger;

/** */
/**
 * 接受线程池管理的线程
 * 
 * @author ryang 2006-8-8
 */
public class PooledThread extends Thread {

    private static final Logger logger = Logger.getLogger(PooledThread.class);

    private Vector<Runnable> tasks = new Vector<Runnable>();

    private boolean inited;

    private boolean running;

    private boolean stopped;

    private boolean paused;

    private boolean killed;

    private ThreadPool pool;

    public PooledThread(ThreadPool pool) {
        this.pool = pool;
    }

    public void putTask(Runnable task) {
        tasks.add(task);
    }

    public void putTasks(Runnable[] tasks) {
        for (int i = 0; i < tasks.length; i++) {
            this.tasks.add(tasks[i]);
        }
    }

    public void putTasks(Collection<Runnable> tasks) {
        this.tasks.addAll(tasks);
    }

    protected Runnable popTask() {
        if (!tasks.isEmpty()) {
            return tasks.remove(0);
        }
        return null;
    }

    public boolean isRunning() {
        return running;
    }

    public void stopTasks() {
        stopped = true;
    }

    public void stopTasksSync() {
        stopTasks();
        while (isRunning()) {
            try {
                sleep(5);
            } catch (InterruptedException e) {
            }
        }
    }

    public void pauseTasks() {
        paused = true;
    }

    public void pauseTasksSync() {
        pauseTasks();
        while (isRunning()) {
            try {
                sleep(5);
            } catch (InterruptedException e) {
            }
        }
    }

    public void kill() {
        if (!running)
            interrupt();
        else
            killed = true;
    }

    public void killSync() {
        kill();
        while (isAlive()) {
            try {
                sleep(5);
            } catch (InterruptedException e) {
            }
        }
    }

    public synchronized void startTasks() {
        running = true;
        this.notify();
    }

    public synchronized void run() {
        try {
            while (true) {
                if (!running) {
                    pool.notifyForIdleThread();
                    this.wait();
                } else {
                    Runnable task;
                    while ((task = popTask()) != null) {
                        try {
                            task.run();
                        } catch (Throwable e) {
                            logger.error("", e);
                        }
                        if (stopped) {
                            stopped = false;
                            if (!tasks.isEmpty()) {
                                tasks.clear();
                                break;
                            }
                        }
                        if (paused) {
                            paused = false;
                            if (!tasks.isEmpty()) {
                                break;
                            }
                        }
                    }
                    running = false;
                }

                if (killed) {
                	System.out.println("杀掉了一个线程");
                    killed = false;
                    break;
                }
            }
        } catch (InterruptedException e) {
            running = false;
            return;
        }
    }

    @Override
    public synchronized void start() {
        if (inited) {
            startTasks();
            return;
        }
        inited = true;
        super.start();
    }
}